﻿using Autofac;
using Bricks.Injecting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Bricks.Autofac.Injecting
{

    /// <summary>
    /// 
    /// </summary>
    public class ContainerFacade : ContainerFacadeBase<IContainer>, IContainerFacade
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        public ContainerFacade(IContainer container) :
            base(container)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public override object Resolve(Type serviceType, string key)
        {
            return Execute<object>(container => container.ResolveNamed(key, serviceType));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public override IEnumerable<object> ResolveAll(Type serviceType)
        {
            return Execute<IEnumerable<object>>(container => ((IEnumerable)container.Resolve(typeof(IEnumerable<>).MakeGenericType(serviceType))).Cast<object>());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public override TService Resolve<TService>(string key)
        {
            return Execute<TService>(container => container.ResolveNamed<TService>(key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public override IEnumerable<TService> ResolveAll<TService>()
        {
            return Execute<IEnumerable<TService>>(container => ((IEnumerable)container.Resolve<IEnumerable<TService>>()).Cast<TService>());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override Bricks.Injecting.IContainerFacade CreateChild()
        {
            throw new NotImplementedException();
        }
    }
}
